package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
//
//@RestController
public class TestController {
    @ResponseBody
    @RequestMapping("/sayhi")
    public String sayHi(){
        return "222";
    }


    //请求转发实现方式1
    @RequestMapping("/fw")
    public String myForward(){
        return "forward:/calc.html";
    }



    //请求转发实现方式1-1
    @RequestMapping("/fw1")
    public String myForward1(){
        return "/calc.html";
    }

    //请求转发实现方式2
    @RequestMapping("/fw2")
    public void myForward2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/login.html").forward(request,response);
    }

    //重定向实现方式1
    @RequestMapping("/rd")
    public String myRedirect(){
        return "redirect:/calc.html";
    }



    //重定向实现方式2
    @RequestMapping("/rd2")
    public void myRedirect2(HttpServletResponse response) throws IOException {
        response.sendRedirect("calc.html");
    }

}
